package com.snailthink.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;

public class JsonUtils {

    /**
     * json格式转换
     * @param object
     * @return
     */
    public static String getJosn(Object object) {
        return getJosn(object, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将对象转为json 格式输出
     *
     * @param object
     * @param dateFormat 时间格式 yyyy-MM-dd HH:mm:ss
     * @return
     * @throws JsonProcessingException
     */
    public static String getJosn(Object object, String dateFormat) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        SimpleDateFormat dtf = new SimpleDateFormat(dateFormat);
        mapper.setDateFormat(dtf);
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


}
